/*******************************************************************************
 * Copyright (c) 2001, 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 ******************************************************************************/
package net.sourceforge.perforce.ui;

import net.sourceforge.perforce.core.api.IPerforceServer;
import net.sourceforge.perforce.ui.internal.Policy;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.team.core.TeamException;
import org.eclipse.ui.dialogs.PropertyPage;

/**
  * @version $Revision: 146 $
  * @author  <a href="bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class PerforceServerInfoPropertyPage extends PropertyPage
{
	// --------------------------------------------------------------------------- Private Variables
	
	/** Stores the Perforce Server. */
	private IPerforceServer perforceServer;
	
	/** Checkbox: Perform background updates */
	private Button backgroundUpdatesButton;
	
	// --------------------------------------------------------------------------- Protected Methods
	
	/** @see PreferencePage#createContents(Composite) */
	protected Control createContents(Composite parent)
	{
		initialize();
		noDefaultAndApplyButton();
		
		Composite composite = new Composite(parent, SWT.NULL);
		composite.setLayoutData(new GridData(GridData.FILL_BOTH));
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		composite.setLayout(layout);
		
		try
		{
			createLabel(composite, 
				Policy.bind("PerforceServerInfoPropertyPage.serverAddress"));          //$NON-NLS-1$
			createLabel(composite, perforceServer.getServerAddress());
	
			createLabel(composite, 
				Policy.bind("PerforceServerInfoPropertyPage.serverRoot"));             //$NON-NLS-1$
			createLabel(composite, perforceServer.getServerRoot());
	
			createLabel(composite, 
				Policy.bind("PerforceServerInfoPropertyPage.serverDate"));             //$NON-NLS-1$
			createLabel(composite, perforceServer.getServerDate());
			
			createLabel(composite, 
				Policy.bind("PerforceServerInfoPropertyPage.serverVersion"));          //$NON-NLS-1$
			createLabel(composite, perforceServer.getServerVersion());
	
			createLabel(composite, 
				Policy.bind("PerforceServerInfoPropertyPage.serverLicense"));          //$NON-NLS-1$
			createLabel(composite, perforceServer.getServerLicense());

			Label separator = new Label(composite, SWT.HORIZONTAL | SWT.SEPARATOR);
			GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
			gridData.horizontalSpan = 2;
			separator.setLayoutData(gridData);
			
			backgroundUpdatesButton = new Button(composite, SWT.CHECK);
			GridData data = new GridData();
			data.horizontalSpan = 2;
			backgroundUpdatesButton.setLayoutData(data);
			backgroundUpdatesButton.setText(
				Policy.bind("PerforceServerInfoPropertyPage.backgroundUpdates"));      //$NON-NLS-1$
			backgroundUpdatesButton.setSelection(perforceServer.shouldIncludeInBackgroundUpdates());
		}
		catch (TeamException e)
		{
			PerforceUIPlugin.log(e.getStatus());
		}
			
		return composite;
	}
	
	/** @see IPreferencePage#performOk() */
	public boolean performOk() 
	{
		boolean value = backgroundUpdatesButton.getSelection();
		perforceServer.setShouldIncludeInBackgroundUpdates(value);
		
		return super.performOk();
	}
	
	// ----------------------------------------------------------------------------- Private Methods

	/** Called to initialize the property page. */
	private void initialize() 
	{
		IAdaptable element = getElement();
		if (element instanceof IPerforceServer) 
		{
			perforceServer = (IPerforceServer) element;
		} 
		else 
		{
			Object adapter = element.getAdapter(IPerforceServer.class);
			if (adapter instanceof IPerforceServer) 
			{
				perforceServer = (IPerforceServer) adapter;
			}
		}
	}	
	
	/** Called to create a label, setting the horizontal span to the given value.
	  *
	  * @param parent the parent composite.
	  * @param text the label's text.
	  * @param span the horizontal span.
	  * 
	  * @return the new Label.
	  */
	private Label createLabel(Composite parent, String text, int span) 
	{
		Label label = new Label(parent, SWT.LEFT);
		label.setText(text);
		GridData data = new GridData();
		data.horizontalSpan = span;
		data.horizontalAlignment = GridData.FILL;
		label.setLayoutData(data);
		return label;
	}
	
	/** Called to create a label, setting the horizontal span to the given one.
	  *
 	  * @param parent the parent composite.
	  * @param text the label's text.
	  * 
	  * @return the new Label. 
	  */
	private Label createLabel(Composite parent, String text) 
	{
		return createLabel(parent, text, 1);
	}	
}
